//
//  StoreListCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/9.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit
import SDWebImage

class StoreListCell: UICollectionViewCell {
    let width = 375//获取屏幕宽
//    var titleLabel:UILabel?//title
    let selView = UIView()
    let selLab = UILabel()
    let logoImg = UIImageView()
    var celldata : StoreListCellModel? {
        didSet{
            loadData()
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initView(){
        self.backgroundColor = .white
        
        // 设置边框
        self.layer.borderWidth = 0.5
        self.layer.borderColor = UIColor.xF7F7F7.cgColor
        self.layer.cornerRadius = 5
        
        //设置阴影颜色
        self.layer.shadowColor = UIColor.xF7F7F7.cgColor
        //设置透明度
        self.layer.shadowOpacity = 1
        //设置阴影半径
        self.layer.shadowRadius = 5
        //设置阴影偏移量
        self.layer.shadowOffset = CGSize(width: 0,height: 0)
        
        drawImage(imageView:logoImg, named: nil) { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-15)
            make.width.equalTo(75)
            make.height.equalTo(70)
        }
//        logoImg.backgroundColor = .green
        
        //画横线
        let line = drawView { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(logoImg.snp.bottom)
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF7F7F7
        
        drawView(view: selView) { (make) in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        selView.drawLabel(label: selLab, text: "", fontSize: 12, textColor: .x666666) { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().offset(-5)
        }
        selLab.textAlignment = .center
        selLab.numberOfLines = 2
    }
    
    func loadData() {
        if var urlstring = celldata?.mallLogo {
            urlstring = (IMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            let url = URL(string: urlstring)
            logoImg.sd_setImage(with:url)
        }else{
            logoImg.image = nil
        }
        selLab.text = celldata?.mallName
    }
    
    override var isSelected: Bool{
        didSet{
            if (isSelected) {
                //选中时
                selView.backgroundColor = .xFC4C02;
                selLab.textColor = .white
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    self.selView.layer.mask = self.configRectCorner(view: self.selView, corner: [.bottomLeft,.bottomRight], radii: CGSize(width: 5, height: 5))
                }
            }else{
                //非选中
                selView.backgroundColor = .white;
                selLab.textColor = .x666666
            }
        }
    }
    
    /// 圆角设置
    ///
    /// - Parameters:
    ///   - view: 需要设置的控件
    ///   - corner: 哪些圆角
    ///   - radii: 圆角半径
    /// - Returns: layer图层
    func configRectCorner(view: UIView, corner: UIRectCorner, radii: CGSize) -> CALayer {
        
        let maskPath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: corner, cornerRadii: radii)
        
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = view.bounds
        maskLayer.path = maskPath.cgPath
        
        return maskLayer
    }
}

//
//    RootClass.swift
//    Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation

struct StoreListCellModel{

    var defaultLoginMall : String?
    var mallID : String!
    var mallLogo : String!
    var mallName : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        defaultLoginMall = dictionary["defaultLoginMall"] as? String
        mallID = dictionary["mallID"] as? String
        mallLogo = dictionary["mallLogo"] as? String
        mallName = dictionary["mallName"] as? String
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if defaultLoginMall != nil{
            dictionary["defaultLoginMall"] = defaultLoginMall
        }
        if mallID != nil{
            dictionary["mallID"] = mallID
        }
        if mallLogo != nil{
            dictionary["mallLogo"] = mallLogo
        }
        if mallName != nil{
            dictionary["mallName"] = mallName
        }
        return dictionary
    }

}
